package com.boot.mgt.controller.web

import com.boot.core.result.ResultTo
import com.boot.mgt.controller.web.vo.EditAccidentVo
import com.boot.mgt.service.TaAccidentService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 事故管理接口
 */
@RestController
@RequestMapping("/admin/accident")
class TaAccidentController {

    @Autowired
    private lateinit var accidentService: TaAccidentService

    /**
     * 获取事故信息列表
     */
    @GetMapping("/list")
    fun list(request: HttpServletRequest,
             page_index: Int?, page_size: Int?): ResultTo {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.apply {
            for (paramMap in this) {
                map[paramMap.key] = paramMap.value[0]
            }
        }
        return accidentService.list(page_index, page_size, map)
    }

    /**
     * 获取事故信息详情
     */
    @GetMapping("/detail")
    fun detail(id: Int): ResultTo {
        return accidentService.detail(id)
    }

    /**
     * 事故信息编辑
     */
    @PostMapping("/edit")
    fun edit(@RequestBody editAccidentVo: EditAccidentVo): ResultTo {
        return accidentService.webEditAccident(editAccidentVo, false)
    }

    /**
     * 导出excel
     */
    @GetMapping("/excel")
    fun outExcel(request: HttpServletRequest, response: HttpServletResponse) {
        val begin = request.getParameter("occur_time_begin")
        val end = request.getParameter("occur_time_end")
        if (!begin.isNullOrBlank() && !end.isNullOrBlank()) {
            val map = hashMapOf<String, Any>()
            request.parameterMap?.apply {
                for (paramMap in this) {
                    map[paramMap.key] = paramMap.value[0]
                }
            }
            accidentService.outExcel(map, response)
        } else {
            log.info("参数错误")
        }
    }


    /**
     * 事故信息编辑
     */
    @GetMapping("/related_acd_file")
    fun bind(id: Int): ResultTo {
        accidentService.relatedAcdFile(5)
        accidentService.relatedLocalFile(5)
        return ResultTo()
    }

    companion object {
        private val log: Logger = LoggerFactory.getLogger(this::class.java)
    }
}

